/*
 * Copyright 2023-2085 xujin <ceo@itgrail.com>
 *
 * Licensed under the Grail License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://itgrail.com/docs?id=3&catalogId=87
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.itgrail.pilot.infrastructure.tunnel.db.dataobject;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;


/**
 * @Author xujin
 * @create 2023/2/25 15:39
 */
@TableName("t_identity_source")
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class IdentitySourceDO implements Cloneable, Serializable {

    private Integer id;

    private String uniqueCode;

    private String name;

    private String icon;

    @TableField(value = "`desc`")
    private String desc;

    private String provider;

    private String basicConfig;

    private String strategyConfig;

    private String jobConfig;

    private Integer configured;

    private String remark;

    private Integer enabled;

    private String isDeleted;

    private LocalDateTime createAt;

    private LocalDateTime updateAt;

    private String createBy;

    private String updateBy;

    private static final long serialVersionUID = 1L;

    @Override
    public IdentitySourceDO clone() throws CloneNotSupportedException {
        return (IdentitySourceDO) super.clone();
    }

    public enum Column {
        id("id", "id", "INT", false),
        name("name", "name", "VARCHAR", false),
        icon("icon", "icon", "LONGVARCHAR", false),
        desc("desc", "desc", "VARCHAR", false),
        provider("provider", "provider", "VARCHAR", false),
        remark("remark", "remark", "VARCHAR", false),
        enabled("enabled", "enabled", "INTEGER", false),
        isDeleted("is_deleted", "isDeleted", "VARCHAR", false),
        createAt("create_at", "createAt", "TIMESTAMP", false),
        updateAt("update_at", "updateAt", "TIMESTAMP", false),
        createBy("create_by", "createBy", "VARCHAR", false),
        updateBy("update_by", "updateBy", "VARCHAR", false),
        ;

        private static final String BEGINNING_DELIMITER = "`";

        private static final String ENDING_DELIMITER = "`";

        private final String column;

        private final boolean isColumnNameDelimited;

        private final String javaProperty;

        private final String jdbcType;

        public String value() {
            return this.column;
        }

        public String getValue() {
            return this.column;
        }

        public String getJavaProperty() {
            return this.javaProperty;
        }

        public String getJdbcType() {
            return this.jdbcType;
        }

        Column(String column, String javaProperty, String jdbcType, boolean isColumnNameDelimited) {
            this.column = column;
            this.javaProperty = javaProperty;
            this.jdbcType = jdbcType;
            this.isColumnNameDelimited = isColumnNameDelimited;
        }

        public String desc() {
            return this.getEscapedColumnName() + " DESC";
        }

        public String asc() {
            return this.getEscapedColumnName() + " ASC";
        }

        public static Column[] excludes(Column... excludes) {
            ArrayList<Column> columns = new ArrayList<>(Arrays.asList(Column.values()));
            if (excludes != null && excludes.length > 0) {
                columns.removeAll(new ArrayList<>(Arrays.asList(excludes)));
            }
            return columns.toArray(new Column[]{});
        }

        public static Column[] all() {
            return Column.values();
        }

        public String getEscapedColumnName() {
            if (this.isColumnNameDelimited) {
                return new StringBuilder().append(BEGINNING_DELIMITER).append(this.column).append(ENDING_DELIMITER).toString();
            } else {
                return this.column;
            }
        }

        public String getAliasedEscapedColumnName() {
            return this.getEscapedColumnName();
        }
    }

    public enum IsDeleted {
        NOT_DELETED(new String("0"), "未删除"),
        IS_DELETED(new String("1"), "已删除");

        private final String value;

        private final String name;

        IsDeleted(String value, String name) {
            this.value = value;
            this.name = name;
        }

        public String getValue() {
            return this.value;
        }

        public String value() {
            return this.value;
        }

        public String getName() {
            return this.name;
        }

        public static IsDeleted parseValue(String value) {
            if (value != null) {
                for (IsDeleted item : values()) {
                    if (item.value.equals(value)) {
                        return item;
                    }
                }
            }
            return null;
        }

        public static IsDeleted parseName(String name) {
            if (name != null) {
                for (IsDeleted item : values()) {
                    if (item.name.equals(name)) {
                        return item;
                    }
                }
            }
            return null;
        }
    }

    public enum Enabled {
        IS_ENABLED(new Integer("1"), "启用"),
        NOT_ENABLED(new Integer("0"), "未启用");

        private final Integer value;

        private final String name;

        Enabled(Integer value, String name) {
            this.value = value;
            this.name = name;
        }

        public Integer getValue() {
            return this.value;
        }

        public Integer value() {
            return this.value;
        }

        public String getName() {
            return this.name;
        }

        public static Enabled parseValue(Integer value) {
            if (value != null) {
                for (Enabled item : values()) {
                    if (item.value.equals(value)) {
                        return item;
                    }
                }
            }
            return null;
        }

        public static Enabled parseName(String name) {
            if (name != null) {
                for (Enabled item : values()) {
                    if (item.name.equals(name)) {
                        return item;
                    }
                }
            }
            return null;
        }
    }

    public enum Provider {
        DING_TALK(new String("ding_talk"), "钉钉"),
        LARK(new String("lark"), "飞书"),
        WEICHART_WORK(new String("weichart_work"), "企业微信");

        private final String value;

        private final String name;

        Provider(String value, String name) {
            this.value = value;
            this.name = name;
        }

        public String getValue() {
            return this.value;
        }

        public String value() {
            return this.value;
        }

        public String getName() {
            return this.name;
        }

        public static Provider parseValue(String value) {
            if (value != null) {
                for (Provider item : values()) {
                    if (item.value.equals(value)) {
                        return item;
                    }
                }
            }
            return null;
        }

        public static Provider parseName(String name) {
            if (name != null) {
                for (Provider item : values()) {
                    if (item.name.equals(name)) {
                        return item;
                    }
                }
            }
            return null;
        }
    }

    public enum Configured {
        Yes(new Integer("1"), "可配置"),
        No(new Integer("0"), "不可配置");

        private final Integer value;

        private final String name;

        Configured(Integer value, String name) {
            this.value = value;
            this.name = name;
        }

        public Integer getValue() {
            return this.value;
        }

        public Integer value() {
            return this.value;
        }

        public String getName() {
            return this.name;
        }

        public static Configured parseValue(Integer value) {
            if (value != null) {
                for (Configured item : values()) {
                    if (item.value.equals(value)) {
                        return item;
                    }
                }
            }
            return null;
        }

        public static Configured parseName(String name) {
            if (name != null) {
                for (Configured item : values()) {
                    if (item.name.equals(name)) {
                        return item;
                    }
                }
            }
            return null;
        }
    }

}
